package com.COE.MdmSdkLib;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;


public class SdkLibManager {
    // 设备队列
    public class MyDeviceClass {
        int handler;
        String manufacturer_id; /**/
        int product_id; /**/
        String device_serial_number;/**/
    }

    ;

    public class MyCmdClass {
        int task_no;
        String manufacturer_id; /**/
        int product_id; /**/
        String device_serial_number;/**/
        String json;
    }

    ;

    // 设备对列
    @SuppressWarnings("unused")
    private Map<Integer, MyDeviceClass> devices = new HashMap<Integer, MyDeviceClass>();
    // 命令队列
    @SuppressWarnings("unused")
    private Map<Integer, MyCmdClass> cmds = new HashMap<Integer, MyCmdClass>();

    static SdkLibListener __sdklib_Listener;
    //static SdkLibListenerImpl __sdklib_Listener;

    private static SdkLibManager m_SdkLibManager;

    private SdkLibManager() {
    }

    public static void set__sdklib_Listener(SdkLibListener __sdklib_Listener) {
        SdkLibManager.__sdklib_Listener = __sdklib_Listener;
    }
    
//	public static void set__sdklib_Listener(SdkLibListenerImpl __sdklib_Listener) {
//	  SdkLibManager.__sdklib_Listener = __sdklib_Listener;
//	}
    	
    public static SdkLibManager getInstance() {
        if (m_SdkLibManager == null) {
            m_SdkLibManager = new SdkLibManager();
        }
        return m_SdkLibManager;
    }

    public int init(String ip, int port) {
        String manufacturer_id = "aaa";
        String manufacturer_license = "aaa";
        int product_id = 111;
        int product_version = 1;
        String device_serial_number = "aaa";
        String device_serial_number_license = "aaa";
        String follow_topics = "aaa";
//        int imid = 13884;
//        return API.InitSdk(__sdklib_Listener, manufacturer_id,
//                            manufacturer_license, product_id,
//                            product_version, device_serial_number,
//                            device_serial_number_license,
//                            follow_topics,
//                            imid,
//                            ip,
//                            port);

        String imid = "long_yc";
        return API.InitSdk(__sdklib_Listener, manufacturer_id,
                manufacturer_license, product_id,
                product_version, device_serial_number,
                device_serial_number_license,
                follow_topics,
                imid,
                ip,
                port);
    }

    public int init(String ip, int port, DeviceInfo deviceInfo) {
        String manufacturer_id = deviceInfo.getManufacturer_id();
        String manufacturer_license = deviceInfo.getManufacturer_license();
        int product_id = deviceInfo.getProduct_id();
        int product_version = deviceInfo.getProduct_version();
        String device_serial_number = deviceInfo.getDevice_serial_number();
        String device_serial_number_license = deviceInfo.getDevice_serial_number_license();
        String follow_topics = deviceInfo.getFollow_topics();
        //String imid = deviceInfo.getImid();
        String imid = "tan";
        return API.InitSdk(__sdklib_Listener, manufacturer_id,
                manufacturer_license, product_id,
                product_version, device_serial_number,
                device_serial_number_license,
                follow_topics,
                imid,
                ip,
                port);
    }

    public static boolean hasInited() {
        return m_SdkLibManager != null;
    }

    public static boolean hasDeinited() {
        return m_SdkLibManager == null;
    }

    public static void deinitSdkLib() {
        if (m_SdkLibManager != null) {
            API.deInitSdk();
            m_SdkLibManager = null;
        }
    }

    @SuppressWarnings("unused")
    private int getKeyByValue(Map<Integer, String> map, String value) {
        Iterator<Entry<Integer, String>> iterator = map.entrySet().iterator();
        int key = 0;
        while (iterator.hasNext()) {
            Entry<Integer, String> entry = iterator.next();
            if (entry.getValue().equals(value)) {
                key = entry.getKey();
                break;
            }
        }
        return key;
    }

    public static void sendimdata(int cmd, int imid, String service_name, byte[] pb_package) {
        API.BaseImSend(cmd, imid, service_name, pb_package);
    }

    //单聊和群聊，都通过这个接口
    //单聊:   cmd = 2001, service_name = mainservice
    //群聊:   cmd = 2011, service_name = groupchatservice
    public static void sendimstrdata(int cmd, String imid, String service_name, byte[] pb_package) {
        API.BaseImstrSend(cmd, imid, service_name, pb_package);
    }
}
